package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.domain.dto.UserOrderMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class ClearCartListener {

    private final ICartService cartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.CART_CLEAR_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstants.TRADE_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MqConstants.ORDER_CREATE_KEY
    ))
    public  void  clearCartListener(UserOrderMessage message){
        // 获取用户ID和商品ID列表
        Long userId = message.getUserId();
        List<Long> itemIds = message.getItemIds();
        // 清理购物车中的指定商品
       cartService.removeByItemIds(userId, itemIds);
    }
}
